﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using He.Common;

namespace He.Web.manage.settings
{
    public partial class category_list : He.Web.UI.Manage
    {
        public int channel_id; //栏目种类
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!int.TryParse(Request.Params["channel_id"] as string, out this.channel_id))
            {
                JscriptMsg(350, 230, "错误提示", "<b>出现错误啦！</b>您要管理的类别种类不明确或参数不正确。", "back", "Error");
                return;
            }
            if (!IsPostBack)
            {
                RptBind();
            }
        }

        #region 赋值操作=================================
        private void RptBind()
        {
            rptList.DataSource = new He.BLL.category().GetList(0, channel_id);
            rptList.DataBind();
        }
        #endregion

        protected void rptList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                Literal LitFirst = (Literal)e.Item.FindControl("LitFirst");
                HiddenField txtlayer = (HiddenField)e.Item.FindControl("txtlayer");
                string LitStyle = "<span style=width:{0}px;text-align:right;display:inline-block;>{1}{2}</span>";
                string LitImg1 = "<img src=../images/folder_open.gif align=absmiddle />";
                string LitImg2 = "<img src=../images/t.gif align=absmiddle />";

                int layer = Convert.ToInt32(txtlayer.Value);
                if (layer == 1)
                {
                    LitFirst.Text = LitImg1;
                }
                else
                {
                    LitFirst.Text = string.Format(LitStyle, layer * 18, LitImg2, LitImg1);
                }
            }
        }

        //删除
        protected void Del_Command(object sender, CommandEventArgs e)
        {
            int id = Convert.ToInt32(e.CommandArgument);
            He.BLL.category bll = new BLL.category();
            He.Model.category model = bll.GetModel(id);
            if (model.is_protected == 1)
            {
                JscriptMsg(350, 230, "操作失败", "<b>错误提示！</b>您删除的内容已被保护，请先取消保护。", "back", "Error");
                return;
            }
            //删除图片
            FileHelper.DeleteFile(model.img_url);
            //删除记录
            bll.Delete(id);
            //保存日志
            SaveLogs("[栏目类别]删除类别：" + model.title);
            JscriptPrint("批量删除成功啦！", Request.RawUrl, "Success");
        }

        //保存排序
        protected void lbtnSave_Click(object sender, EventArgs e)
        {
            He.BLL.category bll = new BLL.category();
            for (int i = 0; i < rptList.Items.Count; i++)
            {
                CheckBox cb = (CheckBox)rptList.Items[i].FindControl("Check");
                int id = int.Parse(((Label)rptList.Items[i].FindControl("lb_id")).Text);
                int sortId;
                if (!int.TryParse(((TextBox)rptList.Items[i].FindControl("txtSortId")).Text.Trim(), out sortId))
                {
                    sortId = 99;
                }
                bll.UpdateField(id, "sort_id=" + sortId.ToString());
            }
            JscriptPrint("排序保存成功啦！", Request.RawUrl, "Success");
        }
    }
}